//
//  PhotoBrowerManager.m
//  Logistics
//
//  Created by 刘峰 on 2018/1/5.
//  Copyright © 2018年 com.Logistics. All rights reserved.
//

#import "PhotoBrowerManager.h"
#import "PhotoBrowserViewController.h"
#import "NewCameraViewController.h"
#import "PhotoBrowerManager.h"

@class PhotoBrowerManager;

@interface PhotoBrowerManager () 

@property (nonatomic, copy) FinishSelectedBlock selectBlock;

@property (nonatomic, strong) ImageChooseFormObject *formObject;

@property (nonatomic, strong) UIAlertController *alertController;

@end

@implementation PhotoBrowerManager

+ (instancetype)defaultManager {
    static PhotoBrowerManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PhotoBrowerManager alloc] init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishSelectedAction:) name:KNotificationDidFinishIMAGE object:nil];
    }
    return self;
}

- (void)finishSelectedAction:(NSNotification *)notification {
    NSArray *images = notification.userInfo.allValues.firstObject;
    
    if (self.selectBlock) {
        self.selectBlock(images);
    }
    
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)muitiImagesForm:(ImageChooseForm)form
               MaxLimit:(NSInteger)limitNum
              CurrentVc:(UIViewController *)currentVc
            FinishBlock:(FinishSelectedBlock)finishBlock {
    
    self.maxLimit = limitNum;
    
    [self jumpViewController:currentVc Mode:PhotoBrowserContentModeAll Form:form];

    
    self.selectBlock = finishBlock;
}


- (void)multipleImages:(NSInteger)limitNum
           ContentMode:(PhotoBrowserContentMode)mode
             CurrentVc:(UIViewController *)currentVc
           FinishBlock:(FinishSelectedBlock)finishBlock {
    
    self.maxLimit = limitNum;
    
    [self jumpViewController:currentVc Mode:mode Form:ImageChooseFormCustom];

    self.selectBlock = finishBlock;
}

- (void)selectAlongPicture:(PhotoBrowserContentMode)mode
                 CurrentVc:(UIViewController *)currentVc
               FinishBlock:(FinishSelectedBlock)finishBlock {
    self.maxLimit = 1;
    [self jumpViewController:currentVc Mode:mode Form:ImageChooseFormCustom];
    self.selectBlock = finishBlock;
}

- (void)jumpViewController:(UIViewController *)currentVc Mode:(PhotoBrowserContentMode)mode Form:(ImageChooseForm)form{
    if (mode == PhotoBrowserContentModeAll) {
        
        [self.formObject showForm:form InViewController:currentVc];

    }else {
        switch (mode) {
            case PhotoBrowserContentModePhoto:{
                PhotoBrowserViewController *photoVc = [[PhotoBrowserViewController alloc] initWithStyle:UITableViewStylePlain];
                [currentVc presentViewController:photoVc navigationbarColor:[HEXCOLOR(0x272e33) colorWithAlphaComponent:0.5]];
            }
                break;
                
            default:{
                NewCameraViewController *cameraVc = [[NewCameraViewController alloc] init];
                [currentVc presentViewController:cameraVc animated:YES completion:nil];
            }
                break;
        }
    }
}


-(ImageChooseFormObject *)formObject {
    if (!_formObject) {
        _formObject = [[ImageChooseFormObject alloc] init];
    }
    return _formObject;
}



@end
